package com.fuint.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuint.framework.annoation.OperationServiceLog;
import com.fuint.framework.exception.BusinessCheckException;
import com.fuint.framework.pagination.PaginationRequest;
import com.fuint.framework.pagination.PaginationResponse;
import com.fuint.repository.model.MtDeviceRecord;
import com.fuint.common.service.DeviceRecordService;
import com.fuint.common.enums.StatusEnum;
import com.fuint.repository.mapper.MtDeviceRecordMapper;
import com.github.pagehelper.PageHelper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.pagehelper.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;

/**
 * 设备记录表服务接口
 *
 * Created by whileu
 * CopyRight https://www.fuint.cn
 */
@Service
@AllArgsConstructor
public class DeviceRecordServiceImpl extends ServiceImpl<MtDeviceRecordMapper, MtDeviceRecord> implements DeviceRecordService {

    private static final Logger logger = LoggerFactory.getLogger(DeviceRecordServiceImpl.class);

    private MtDeviceRecordMapper mtDeviceRecordMapper;

    /**
     * 分页查询数据列表
     *
     * @param paginationRequest
     * @return
     */
    @Override
    public PaginationResponse<MtDeviceRecord> queryDeviceRecordListByPagination(PaginationRequest paginationRequest) {
        Page<MtDeviceRecord> pageHelper = PageHelper.startPage(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
        LambdaQueryWrapper<MtDeviceRecord> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.ne(MtDeviceRecord::getStatus, StatusEnum.DISABLE.getKey());

        lambdaQueryWrapper.orderByAsc(MtDeviceRecord::getId);
        List<MtDeviceRecord> dataList = mtDeviceRecordMapper.selectList(lambdaQueryWrapper);

        PageRequest pageRequest = PageRequest.of(paginationRequest.getCurrentPage(), paginationRequest.getPageSize());
        PageImpl pageImpl = new PageImpl(dataList, pageRequest, pageHelper.getTotal());
        PaginationResponse<MtDeviceRecord> paginationResponse = new PaginationResponse(pageImpl, MtDeviceRecord.class);
        paginationResponse.setTotalPages(pageHelper.getPages());
        paginationResponse.setTotalElements(pageHelper.getTotal());
        paginationResponse.setContent(dataList);

        return paginationResponse;
    }

    /**
     * 添加设备记录表
     *
     * @param mtDeviceRecord 设备记录表信息
     * @return
     */
    @Override
    @OperationServiceLog(description = "新增设备记录表")
    public MtDeviceRecord addDeviceRecord(MtDeviceRecord mtDeviceRecord) throws BusinessCheckException {
        mtDeviceRecord.setStatus(StatusEnum.ENABLED.getKey());
        mtDeviceRecord.setUpdateTime(new Date());
        mtDeviceRecord.setCreateTime(new Date());
        Integer id = mtDeviceRecordMapper.insert(mtDeviceRecord);
        if (id > 0) {
            return mtDeviceRecord;
        } else {
            throw new BusinessCheckException("新增设备记录表数据失败");
        }
    }

    /**
     * 根据ID获设备记录表取息
     *
     * @param id 设备记录表ID
     * @return
     */
    @Override
    public MtDeviceRecord queryDeviceRecordById(Integer id) {
        return mtDeviceRecordMapper.selectById(id);
    }

    /**
     * 根据ID删除设备记录表
     *
     * @param id 设备记录表ID
     * @param operator 操作人
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @OperationServiceLog(description = "删除设备记录表")
    public void deleteDeviceRecord(Integer id, String operator) {
        MtDeviceRecord mtDeviceRecord = queryDeviceRecordById(id);
        if (null == mtDeviceRecord) {
            return;
        }
        mtDeviceRecord.setStatus(StatusEnum.DISABLE.getKey());
        mtDeviceRecord.setUpdateTime(new Date());
        mtDeviceRecordMapper.updateById(mtDeviceRecord);
    }

    /**
     * 修改设备记录表数据
     *
     * @param mtDeviceRecord
     * @throws BusinessCheckException
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @OperationServiceLog(description = "更新设备记录表")
    public MtDeviceRecord updateDeviceRecord(MtDeviceRecord mtDeviceRecord) throws BusinessCheckException {
        mtDeviceRecord = queryDeviceRecordById(mtDeviceRecord.getId());
        if (mtDeviceRecord == null) {
            throw new BusinessCheckException("该设备记录表状态异常");
        }
        mtDeviceRecord.setUpdateTime(new Date());
        mtDeviceRecordMapper.updateById(mtDeviceRecord);
        return mtDeviceRecord;
    }

   /**
    * 根据条件搜索设备记录表
    *
    * @param  params 查询参数
    * @throws BusinessCheckException
    * @return
    * */
    @Override
    public List<MtDeviceRecord> queryDeviceRecordListByParams(Map<String, Object> params) {
        String status =  params.get("status") == null ? StatusEnum.ENABLED.getKey(): params.get("status").toString();
        String storeId =  params.get("storeId") == null ? "" : params.get("storeId").toString();
        String merchantId =  params.get("merchantId") == null ? "" : params.get("merchantId").toString();

        LambdaQueryWrapper<MtDeviceRecord> lambdaQueryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(status)) {
            lambdaQueryWrapper.eq(MtDeviceRecord::getStatus, status);
        }
//        if (StringUtils.isNotBlank(merchantId)) {
//            lambdaQueryWrapper.eq(MtDeviceRecord::getMerchantId, merchantId);
//        }
//        if (StringUtils.isNotBlank(storeId)) {
//            lambdaQueryWrapper.and(wq -> wq
//                    .eq(MtDeviceRecord::getStoreId, 0)
//                    .or()
//                    .eq(MtDeviceRecord::getStoreId, storeId));
//        }

        lambdaQueryWrapper.orderByAsc(MtDeviceRecord::getId);
        List<MtDeviceRecord> dataList = mtDeviceRecordMapper.selectList(lambdaQueryWrapper);
        return dataList;
    }
}
